package core.schedule.snake.data.mydao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


public class BaseDAO  
{  
    	
	private  Datasource datasource;
	private static String driverClassName = "";  
    private static String username = "";  
    private static String password = "";  
    private static String url = "";  
 
    protected Date getSqlOfUtil(java.util.Date dd) {
    	if(dd==null) return null;
    	return new Date(dd.getTime());
    }
    
    
	public void setDatasource(Datasource datasource) {
		this.datasource=datasource;
		this.driverClassName=datasource.getDriverClassName();
		this.username=datasource.getUsername();
		this.password=datasource.getPassword();
		this.url=datasource.getUrl();
	}

	@SuppressWarnings("unchecked")  
    protected List query(String sql, RowMapper rowMapper)  
    {  
        Connection con = null;  
        Statement stmt = null;  
        ResultSet rs = null;  
        List list = new ArrayList();  
        try 
        {  
            Class.forName(driverClassName);  
            con = DriverManager.getConnection(url, username, password);  
            stmt = con.createStatement();  
            rs = stmt.executeQuery(sql);  
            while (rs.next())  
            {  
                list.add(rowMapper.map(rs));  
            }  
        }  
        catch (ClassNotFoundException e)  
        {  
            e.printStackTrace();  
        }  
        catch (SQLException e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
            DBUtil.closeQuietly(rs);  
            DBUtil.closeQuietly(stmt);  
            DBUtil.closeQuietly(con);  
        }  
        return list;  
    }  
 
    @SuppressWarnings("unchecked")  
    protected List query(String sql, final Object[] objs, RowMapper rowMapper)  
    {  
        Connection con = null;  
        PreparedStatement preparedStatement = null;  
        ResultSet rs = null;  
        List list = new ArrayList();  
        try 
        {  
            Class.forName(driverClassName);  
            con = DriverManager.getConnection(url, username, password);  
            preparedStatement = con.prepareStatement(sql);  
            if (objs != null && objs.length > 0)  
            {  
                for (int i = 0, n = objs.length; i < n; i++)  
                {  
                    preparedStatement.setObject(i + 1, objs[i]);  
                }  
            }  
            rs = preparedStatement.executeQuery();  
            while (rs.next())  
            {  
                list.add(rowMapper.map(rs));  
            }  
        }  
        catch (SQLException e)  
        {  
            throw new RuntimeException(e);  
        }  
        catch (ClassNotFoundException e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
            DBUtil.closeQuietly(rs);  
            DBUtil.closeQuietly(preparedStatement);  
            DBUtil.closeQuietly(con);  
        }  
        return list;  
    }  
 

    protected int update(final String sql, final Object[] objs)  
    {  
        Connection con = null;  
        PreparedStatement preparedStatement = null;  
        int retCode = 0;  
        try 
        {  
            Class.forName(driverClassName);  
            con = DriverManager.getConnection(url, username, password);  
            preparedStatement = con.prepareStatement(sql);  
            if (objs != null && objs.length > 0)  
            {  
                for (int i = 0, n = objs.length; i < n; i++)  
                {  
                  preparedStatement.setObject(i + 1, objs[i]);  

//                	if(objs[i]==null || !(objs[i] instanceof java.util.Date)) {
//                		System.out.println(objs[i]);
//                        preparedStatement.setObject(i + 1, objs[i]);  
//                	}else {
//                		java.util.Date date=(java.util.Date) objs[i];
//                		String d=DateHelper.getFormatDate(date, "yyyy-MM-dd hh:mm:ss");
//                		String ss="to_date('"+d+"','yyyy-mm-dd hh24:mi:ss')";
//                		System.out.println(ss);
//                		preparedStatement.setString(i + 1, ss);
//                	}
                }  
            }  
            retCode = preparedStatement.executeUpdate();  
        }  
        catch (SQLException e)  
        {  
        	e.printStackTrace();
            throw new RuntimeException(e);  
        }  
        catch (ClassNotFoundException e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
            DBUtil.closeQuietly(preparedStatement);  
            DBUtil.closeQuietly(con);  
        }  
        return retCode;  
    }  
 
    protected int update(final String sql)  
    {  
        Connection con = null;  
        Statement stmt = null;  
        int retCode = 0;  
        try 
        {  
            Class.forName(driverClassName);  
            con = DriverManager.getConnection(url, username, password);  
            stmt = con.createStatement();  
            retCode = stmt.executeUpdate(sql);  
        }  
        catch (SQLException e)  
        {  
            throw new RuntimeException(e);  
        }  
        catch (ClassNotFoundException e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
            DBUtil.closeQuietly(stmt);  
            DBUtil.closeQuietly(con);  
        }  
        return retCode;  
    }  
 
    protected int[] batchUpdate(final String[] sql)  
    {  
        Connection con = null;  
        Statement stmt = null;  
        int[] rowsAffected = new int[sql.length];  
        try 
        {  
            Class.forName(driverClassName);  
            con = DriverManager.getConnection(url, username, password);  
            stmt = con.createStatement();  
            for (int i = 0, n = sql.length; i < n; i++)  
            {  
                stmt.addBatch(sql[i]);  
            }  
            rowsAffected = stmt.executeBatch();  
        }  
        catch (SQLException e)  
        {  
            throw new RuntimeException(e);  
        }  
        catch (ClassNotFoundException e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
            DBUtil.closeQuietly(stmt);  
            DBUtil.closeQuietly(con);  
        }  
        return rowsAffected;  
    }  
 

    public int[] batchUpdate(final String sql, final BatchPreparedStatementSetter pss)  
    {  
 
        Connection con = null;  
        PreparedStatement preparedStatement = null;  
        int[] rowsAffected = null;  
        try 
        {  
            Class.forName(driverClassName);  
            con = DriverManager.getConnection(url, username, password);  
            preparedStatement = con.prepareStatement(sql);  
            for (int i = 0, n = pss.getBatchSize(); i < n; i++)  
            {  
                pss.setValues(preparedStatement, i);  
                preparedStatement.addBatch();  
            }  
            rowsAffected = preparedStatement.executeBatch();  
        }  
        catch (SQLException e)  
        {  
            throw new RuntimeException(e);  
        }  
        catch (ClassNotFoundException e)  
        {  
            e.printStackTrace();  
        }  
        finally 
        {  
            DBUtil.closeQuietly(preparedStatement);  
            DBUtil.closeQuietly(con);  
        }  
        return rowsAffected;  
    }

	public String getDriverClassName() {
		return driverClassName;
	}

	public void setDriverClassName(String driverClassName) {
		this.driverClassName = driverClassName;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Datasource getDatasource() {
		return datasource;
	}  
	
    
} 
